package com.yuyu.controller;

import com.yuyu.bean.Classify;
import com.yuyu.bean.PageData;
import com.yuyu.bean.PageDataOfMybatis;
import com.yuyu.bean.ResultData;
import com.yuyu.service.ClassifyService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    @RequestMapping("/findAllClassify")
    @ResponseBody
    public List<Classify> findAllClassify(){
        List<Classify> classifies = classifyService.findAllClassify();
        System.out.println(classifies);
        return classifies;
    }

    //只是跳转页面
    @RequestMapping("/category")
    public String category(){
        return "category2";
    }

    //批量删除
    @RequestMapping("/deleteClassifys")
    @ResponseBody
    public Map deleteClassifys(@RequestBody List<Integer> list){

        System.out.println("前台Ajax传递过来的参数: "+ list);


        int result = classifyService.deleteClassifys(list);
        Map<String, Object> map = new HashMap<>();
        map.put("state_code", result);

        return map;

    }

    @RequestMapping("/addClassify")
    @ResponseBody
    public Map addClassify(Classify classify){
        System.out.println("前台Ajax传递过来的参数: "+ classify);
        int result = classifyService.addClassify(classify);
        Map<String, Object> map = new HashMap<>();
        map.put("state_code", result);
        map.put("father_code", classify.getFather_code());
        map.put("name", classify.getName());
        map.put("code", classifyService.getLastInsertCode());
//        Integer i = classifyService.getLastInsertCode();
//        System.out.println(i);
        return map;

    }

    @RequestMapping("/deleteClassify")
    @ResponseBody
    public Map deleteClassify(Integer code){
        System.out.println("前台Ajax传递过来的参数: "+ code);
        int result = classifyService.deleteClassify(code);
        Map<String, Object> map = new HashMap<>();
        map.put("code", result);

        return map;

    }
    //获得一页的数据

    @RequestMapping("/getOnePageList")
    @ResponseBody
    public ResultData<List<Classify>> getOnePageList(Integer index){
        PageData pageData = new PageData();
        pageData.setPageNumber(index);
        pageData.setPageLimit(PageData.pageRowNumber);
//        PageDataOfMybatis pageDataOfMybatis = PageDataOfMybatis.handle(pageData);
//        System.out.println(pageDataOfMybatis);
        List<Classify> classifies = classifyService.getOnePageList(PageDataOfMybatis.handle(pageData));
        ResultData<List<Classify>> resultData = new ResultData<>();
        resultData.setData(classifies);
        resultData.setCount(classifyService.getCowsCount());
        resultData.setMsg("一页的数据");
        resultData.setCode(1);
        resultData.setAddtionData(PageData.pageRowNumber);
        System.out.println(resultData.getData());
        return resultData;
    }



}
